package com.southminority.ethnic.mapper.contact;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.pojo.ContactInfo;
import com.southminority.ethnic.controller.contact.vo.ContactInfoVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 联系信息Mapper
 * 
 * @author SouthMinority
 */
@Mapper
public interface ContactInfoMapper extends BaseMapper<ContactInfo> {
    
    // 继承MyBatis Plus的BaseMapper，提供基础CRUD能力
    // 需要自定义查询方法时在这里添加
    
    /**
     * 根据ID查询联系信息（包含地图位置信息）
     */
    @Select("SELECT ci.*, mlc.* FROM contact_info ci " +
            "LEFT JOIN map_location_config mlc ON ci.map_location_id = mlc.id " +
            "WHERE ci.id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "type", column = "type"),
        @Result(property = "title", column = "title"),
        @Result(property = "icon", column = "icon"),
        @Result(property = "content", column = "content"),
        @Result(property = "description", column = "description"),
        @Result(property = "organizationId", column = "organization_id"),
        @Result(property = "isPrimary", column = "is_primary"),
        @Result(property = "isPublic", column = "is_public"),
        @Result(property = "displayOrder", column = "display_order"),
        @Result(property = "sortOrder", column = "sort_order"),
        @Result(property = "status", column = "status"),
        @Result(property = "mapLocationId", column = "map_location_id"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time"),
        @Result(property = "mapLocation", column = "map_location_id", 
                one = @One(select = "com.southminority.ethnic.mapper.MapLocationConfigMapper.selectById", 
                          fetchType = FetchType.LAZY))
    })
    ContactInfo selectByIdWithMapLocation(@Param("id") Long id);
    
    /**
     * 查询联系信息列表（包含地图位置信息）
     */
    @Select("<script>" +
            "SELECT ci.id, ci.type, ci.title, ci.icon, ci.content, ci.description, " +
            "ci.organization_id, ci.is_primary, ci.is_public, ci.display_order, " +
            "ci.sort_order, ci.status, ci.map_location_id, ci.create_time, ci.update_time, " +
            "mlc.id as map_id, mlc.name as map_name, mlc.title as map_title, " +
            "mlc.description as map_description, mlc.longitude, mlc.latitude, " +
            "mlc.zoom_level, mlc.pitch, mlc.marker_icon, mlc.info_window_content, " +
            "mlc.is_default as map_is_default, mlc.is_active as map_is_active, " +
            "mlc.sort_order as map_sort_order, mlc.organization_id as map_organization_id, " +
            "mlc.create_time as map_create_time, mlc.update_time as map_update_time " +
            "FROM contact_info ci " +
            "LEFT JOIN map_location_config mlc ON ci.map_location_id = mlc.id " +
            "WHERE ci.status != -1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (ci.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR ci.description LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='type != null and type != \"\"'>" +
            "AND ci.type = #{type} " +
            "</if>" +
            "<if test='status != null'>" +
            "AND ci.status = #{status} " +
            "</if>" +
            "ORDER BY ci.display_order ASC, ci.sort_order ASC, ci.create_time DESC" +
            "</script>")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "type", column = "type"),
        @Result(property = "title", column = "title"),
        @Result(property = "icon", column = "icon"),
        @Result(property = "content", column = "content"),
        @Result(property = "description", column = "description"),
        @Result(property = "organizationId", column = "organization_id"),
        @Result(property = "isPrimary", column = "is_primary"),
        @Result(property = "isPublic", column = "is_public"),
        @Result(property = "displayOrder", column = "display_order"),
        @Result(property = "sortOrder", column = "sort_order"),
        @Result(property = "status", column = "status"),
        @Result(property = "mapLocationId", column = "map_location_id"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time"),
        @Result(property = "mapLocation.id", column = "map_id"),
        @Result(property = "mapLocation.name", column = "map_name"),
        @Result(property = "mapLocation.title", column = "map_title"),
        @Result(property = "mapLocation.description", column = "map_description"),
        @Result(property = "mapLocation.longitude", column = "longitude"),
        @Result(property = "mapLocation.latitude", column = "latitude"),
        @Result(property = "mapLocation.zoomLevel", column = "zoom_level"),
        @Result(property = "mapLocation.pitch", column = "pitch"),
        @Result(property = "mapLocation.markerIcon", column = "marker_icon"),
        @Result(property = "mapLocation.infoWindowContent", column = "info_window_content"),
        @Result(property = "mapLocation.isDefault", column = "map_is_default"),
        @Result(property = "mapLocation.isActive", column = "map_is_active"),
        @Result(property = "mapLocation.sortOrder", column = "map_sort_order"),
        @Result(property = "mapLocation.organizationId", column = "map_organization_id"),
        @Result(property = "mapLocation.createTime", column = "map_create_time"),
        @Result(property = "mapLocation.updateTime", column = "map_update_time")
    })
    IPage<ContactInfo> selectPageWithMapLocation(Page<ContactInfo> page, 
                                                @Param("keyword") String keyword,
                                                @Param("type") String type, 
                                                @Param("status") Integer status);
    
    /**
     * 查询公开的联系信息列表（包含地图位置信息）
     */
    @Select("SELECT ci.*, mlc.* FROM contact_info ci " +
            "LEFT JOIN map_location_config mlc ON ci.map_location_id = mlc.id " +
            "WHERE ci.status = 1 AND ci.is_public = 1 " +
            "ORDER BY ci.display_order ASC, ci.sort_order ASC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "type", column = "type"),
        @Result(property = "title", column = "title"),
        @Result(property = "icon", column = "icon"),
        @Result(property = "content", column = "content"),
        @Result(property = "description", column = "description"),
        @Result(property = "organizationId", column = "organization_id"),
        @Result(property = "isPrimary", column = "is_primary"),
        @Result(property = "isPublic", column = "is_public"),
        @Result(property = "displayOrder", column = "display_order"),
        @Result(property = "sortOrder", column = "sort_order"),
        @Result(property = "status", column = "status"),
        @Result(property = "mapLocationId", column = "map_location_id"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time"),
        @Result(property = "mapLocation", column = "map_location_id", 
                one = @One(select = "com.southminority.ethnic.mapper.MapLocationConfigMapper.selectById", 
                          fetchType = FetchType.LAZY))
    })
    List<ContactInfo> selectPublicWithMapLocation();
    
} 
